package com.wxy.ej.flow.modules.taskflow.service.nodeExecuteStrategy.strategy.nosql;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.config.SpringContextBaseHolder;
import com.rt.easyjava.common.utils.others.StringUtils;
import com.wxy.ej.flow.modules.taskflow.dto.DataDto;
import com.wxy.ej.flow.modules.taskflow.dto.NodeDto;
import com.wxy.ej.flow.modules.taskflow.facade.CollectionDataApi;
import com.wxy.ej.flow.modules.taskflow.service.nodeExecuteStrategy.NodeStrategy;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class NosqlSaveStrategy extends NodeStrategy {

    @Override
    public Result action(NodeDto node, JSONObject flowDataJson, DataDto dataDto) throws Exception{
        String uiStorageDataMapKeyVar = node.getDefineVarName();
        if (StringUtils.isNull(uiStorageDataMapKeyVar)) {
            uiStorageDataMapKeyVar = "dataList";
        }

        CollectionDataApi collectionDataApi = SpringContextBaseHolder.getFeignBean("collectionDataApi", CollectionDataApi.class);

        // 需要存储的数据
        Object uiInParam = dataDto.getUiInParam();
        String  paramString= JSON.toJSONString(uiInParam);
        JSONObject jsonObject = JSONObject.parseObject(paramString);
        jsonObject.put("_businessKey", ""+dataDto.getTaskFlowId());
        jsonObject.put("_description", node.getDescription());

        // 指定mongodb集合
        String collectionName = "collection_"+dataDto.getTaskFlowId();

        // save api
        String key = node.getRemark();
        collectionDataApi.saveJsonUnique(jsonObject, collectionName, key);

        return Result.builder();
    }
}
